﻿using System;
using System.Collections.Generic;
using System.Web;
using ChapterX.Data;
using ChapterX.Data.ChaptersDSTableAdapters;
using System.Data;


namespace ChapterX.BusinessFacade
{
    public class ChapterSystem
    {
        private ChaptersDS.ChaptersDataTable _chapterSchema = null;
        public ChaptersDS.ChaptersDataTable ChapterSchema
        {
            get
            {
                if (null == _chapterSchema)
                {
                    _chapterSchema = new ChaptersDS.ChaptersDataTable();
                }
                return _chapterSchema;
            }
        }

        #region Chapter
        public ChaptersDS.ChaptersRow GetChapterByChapterId(int chapterId)
        {
            ChaptersTableAdapter adapter = new ChaptersTableAdapter();
            ChaptersDS.ChaptersDataTable table = adapter.GetChapterByChapterId(chapterId);

            if (null != table && table.Rows.Count > 0)
            {
                return table[0];
            }
            else
            {
                return null;
            }
        }

        public bool SubmitChapter_Default(ChaptersDS.ChaptersRow chapter)
        {
            ChaptersTableAdapter chapterAdapter = new ChaptersTableAdapter();
            bool result = false;
            try
            {
                //filename 格式： seq_title_other 
                //TODO: seq无法根据文件个数增长，文件名末尾加上随机因子？？？
                int seq = chapter.Seq;
                //String title = chapter.FilePath.Substring(0, chapter.FilePath.LastIndexOf("."));
                //if (title.Contains("-") || title.Contains("_") || title.Contains("—") || title.Contains("——"))
                //{
                //    String[] splitresult = title.Split(new String[] { "-", "-", "——", "—" }, StringSplitOptions.RemoveEmptyEntries);
                //    if (splitresult.Length >= 2 && Int32.TryParse(splitresult[0], out seq) && seq <= 1000)
                //    {
                //        title = splitresult[1].Trim();
                //    }
                //}

                ChaptersDS.ChaptersDataTable table = chapterAdapter.GetChapterByChapterId(chapter.ChapterId);

                if (table.Rows.Count <= 0)
                {
                    //Chapter的更新和DigitizationStatus的更新独立

                    //插入一条chapter记录
                    //插入一条新的digitization log：？？？
                    return InsertChapter(chapterAdapter, chapter);
                }
                else
                {
                    return UpdateChapter(chapter);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public bool UpdateChapter(ChaptersDS.ChaptersRow chapter)
        {
            bool result = false;

            try
            {
                ChaptersTableAdapter adapter = new ChaptersTableAdapter();
                int r = adapter.UpdateChapter(
                    chapter.ChapterId,
                    chapter.Seq,
                    chapter.Title,
                    chapter.IsDescsNull() ? null : chapter.Descs,
                    chapter.SubmitTime);

                if (r > 0)
                    result = true;
            }
            catch (Exception e)
            {
                result = false;
            }

            return result;
        }

        public bool InsertChapter(ChaptersTableAdapter adapter, ChaptersDS.ChaptersRow chapter)
        {
            bool result = false;

            try
            {
                int? chapterId = null;
                adapter.InsertChapter(ref chapterId, 
                    chapter.BookId, 
                    chapter.Seq, 
                    chapter.Title, 
                    chapter.IsDescsNull() ? null : chapter.Descs, 
                    chapter.FilePath, 
                    DateTime.Now);

                if (chapterId.HasValue)
                {
                    chapter.ChapterId = chapterId.Value;
                    result = true;
                }
            }
            catch(Exception ex)
            {
                result = false;
            }

            return result;
        }

        #endregion


    }
}
